/* eslint-disable no-unused-vars */
// 供应商列表
import React from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';
import HOCSelect from '@/components/AntDesign/Select';

const { Option } = Select;

@connect(({ publicdata: { nsSupplyList, nsSupplyFsValue }, loading }) => ({
  nsSupplyList,
  nsSupplyFsValue,
  loading: loading.effects['publicdata/fetchNsSupplyList'],
}))
class Supplier extends React.Component {
  componentDidMount() {
    const { dispatch, params = {} } = this.props;
    dispatch({
      type: 'publicdata/fetchNsSupplyList',
      payload: { name: null, ...params },
    });
  }

  componentWillUnmount() {
    const { dispatch, isClearData = true } = this.props;
    if (isClearData) {
      dispatch({
        type: 'publicdata/updateModelData',
        payload: { nsSupplyList: [] },
      });
    }
  }

  getData = (value, options) => {};

  // 更改当前选择的值并供给父组件用
  triggerChange(changeValue, options) {
    const { onChange, dispatch, ...rest } = this.props;
    if (onChange) {
      onChange(changeValue, options);
    }
    dispatch({
      type: 'publicdata/updateModelData',
      payload: {
        nsSupplyFsValue: rest.labelInValue ? { key: options.key, label: options.title } : options,
      },
    });
  }

  render() {
    const {
      nsSupplyList = [],
      loading,
      getData = this.getData,
      value,
      nsSupplyFsValue,
      ...rest
    } = this.props;
    return (
      <HOCSelect
        placeholder="请输入关键字检索"
        {...rest}
        showSearch
        value={value ? (rest.labelInValue && nsSupplyFsValue) || nsSupplyFsValue.title : value}
        optionFilterProp="children"
        onSelect={(selectValue, options) => getData(selectValue, options)}
        notFoundContent={loading ? <Spin size="small" /> : '暂无数据'}
        onChange={(changeValue, options) => this.triggerChange(changeValue, options)}
      >
        {nsSupplyList.map(item => (
          <Option key={item.suppId} fsOptions={item} title={item.name}>
            {item.name}
          </Option>
        ))}
      </HOCSelect>
    );
  }
}
export default Supplier;
